summaryrefslogtreecommitdiff
path: root/app/api/vendor-investigations/[investigationId]/attachments/route.ts
blob: 6787de7e7cd0af6e89123ae039ba43c1111b6c55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// app/api/vendor-investigations/[investigationId]/attachments/route.ts
import { NextRequest, NextResponse } from "next/server"
import { promises as fs } from "fs"
import path from "path"
import { v4 as uuidv4 } from "uuid"
import db from "@/db/db"
import { vendorInvestigationAttachments } from "@/db/schema"
import { eq } from "drizzle-orm";

type Ctx = { params: Promise<{ investigationId: string }> };

export async function POST(
  req: NextRequest,
  context: Ctx               // ① 두 번째 인자를 통째로 받는다
) {
  try {
    const { investigationId: idParam } = await context.params; // ② 여기서 await
    const investigationId = Number(idParam);
    
    if (!investigationId) {
      return NextResponse.json({ error: "Invalid investigation ID" }, { status: 400 })
    }

    const formData = await req.formData()
    const file = formData.get("file") as File

    if (!file || file.size === 0) {
      return NextResponse.json({ error: "No file provided" }, { status: 400 })
    }

    // 파일 크기 제한 (10MB)
    const maxSize = 10 * 1024 * 1024
    if (file.size > maxSize) {
      return NextResponse.json({
        error: `파일 크기가 ${maxSize / 1024 / 1024}MB를 초과합니다.`
      }, { status: 400 })
    }

    // 지원하는 파일 타입 검증
    const allowedTypes = [
      'application/pdf',
      'application/msword',
      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
      'application/vnd.ms-excel',
      'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'image/png',
      'image/jpeg',
      'image/jpg',
      'image/gif'
    ]

    if (!allowedTypes.includes(file.type)) {
      return NextResponse.json({
        error: "지원하지 않는 파일 형식입니다."
      }, { status: 400 })
    }

    // 파일 저장
    const arrayBuffer = await file.arrayBuffer()
    const buffer = Buffer.from(arrayBuffer)

    // 고유한 파일명 생성
    const ext = path.extname(file.name)
    const uniqueName = `${uuidv4()}${ext}`

    // 업로드 디렉토리 생성
    const uploadDir = path.join(process.cwd(), "public", "vendor-investigation", String(investigationId))
    await fs.mkdir(uploadDir, { recursive: true })

    const filePath = path.join(uploadDir, uniqueName)
    await fs.writeFile(filePath, buffer)

    // 파일 타입 결정
    let attachmentType = "OTHER"
    if (file.type.includes("pdf")) {
      attachmentType = "REPORT"
    } else if (file.type.includes("image")) {
      attachmentType = "PHOTO"
    } else if (
      file.type.includes("word") ||
      file.type.includes("document") ||
      file.name.toLowerCase().includes("report")
    ) {
      attachmentType = "DOCUMENT"
    }

    // DB에 파일 정보 저장
    const [attachment] = await db.insert(vendorInvestigationAttachments).values({
      investigationId,
      fileName: file.name,
      filePath: `/vendor-investigation/${investigationId}/${uniqueName}`,
      fileSize: file.size,
      mimeType: file.type,
      attachmentType: attachmentType as "REPORT" | "PHOTO" | "DOCUMENT" | "CERTIFICATE" | "OTHER",
      // uploadedBy: currentUserId, // 실제 사용자 ID로 설정
    }).returning()

    return NextResponse.json({
      success: true,
      attachment: {
        id: attachment.id,
        fileName: attachment.fileName,
        filePath: attachment.filePath,
        fileSize: attachment.fileSize,
        attachmentType: attachment.attachmentType
      }
    })

  } catch (error) {
    console.error("파일 업로드 오류:", error)
    return NextResponse.json({
      error: "파일 업로드 중 오류가 발생했습니다."
    }, { status: 500 })
  }
}

// 첨부파일 삭제
export async function DELETE(
  req: NextRequest,
  { params }: { params: { investigationId: string } }
) {
  try {
    const { searchParams } = new URL(req.url)
    const attachmentId = searchParams.get("attachmentId")

    if (!attachmentId) {
      return NextResponse.json({ error: "Attachment ID required" }, { status: 400 })
    }

    // DB에서 파일 정보 조회
    const attachment = await db.query.vendorInvestigationAttachments.findFirst({
      where: (attachments, { eq, and }) => and(
        eq(attachments.id, parseInt(attachmentId)),
        eq(attachments.investigationId, parseInt(params.investigationId))
      )
    })

    if (!attachment) {
      return NextResponse.json({ error: "Attachment not found" }, { status: 404 })
    }

    // 실제 파일 삭제
    const fullPath = path.join(process.cwd(), "public", attachment.filePath)
    try {
      await fs.unlink(fullPath)
    } catch (error) {
      console.warn("파일 삭제 실패 (파일이 존재하지 않을 수 있음):", error)
    }

    // DB에서 레코드 삭제
    await db.delete(vendorInvestigationAttachments)
      .where(eq(vendorInvestigationAttachments.id, parseInt(attachmentId)))

    return NextResponse.json({ success: true })

  } catch (error) {
    console.error("파일 삭제 오류:", error)
    return NextResponse.json({
      error: "파일 삭제 중 오류가 발생했습니다."
    }, { status: 500 })
  }
}